//
//  STFeedListView.swift
//  Story
//
//  Created by 汤泽川 on 2019/4/13.
//  Copyright © 2019 汤泽川. All rights reserved.
//

import UIKit
import GTMRefresh
import PKHUD
import RxSwift
import RxCocoa

protocol STFeedListViewDelegate : NSObjectProtocol {
    func didClickCell(model : STStoryInfo)
}

class STFeedListView: UIView, UITableViewDelegate {
    public let feedTableView = UITableView()
    public weak var listViewDelegate : STFeedListViewDelegate?
    private let disposeBag = DisposeBag()
    convenience init(serviceManager : STStoryServiceManager) {
        self.init(frame: CGRect.zero)
        setupTableView(serviceManager : serviceManager)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.white
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupTableView(serviceManager : STStoryServiceManager) {
        let headerRefreshObserver = PublishSubject<Void>()
        let footerRefreshObserver = PublishSubject<Void>()
        self.addSubview(self.feedTableView)
        self.feedTableView.delegate = self
        self.feedTableView.separatorStyle = .none
        self.feedTableView.backgroundColor = UIColor.init(white: 0, alpha: 0.03)
        self.feedTableView.gtm_addRefreshHeaderView {
            headerRefreshObserver.onNext(())
        }
        self.feedTableView.gtm_addLoadMoreFooterView {
            footerRefreshObserver.onNext(())
        }
        self.feedTableView.rx.modelSelected(STStoryInfo.self).subscribe(onNext: {[weak self] (storyInfo) in
            self?.listViewDelegate?.didClickCell(model: storyInfo)
        }).disposed(by: disposeBag)

        let viewModel = STFeedListViewModel(input: (headerRefresh: headerRefreshObserver, footerRefresh: footerRefreshObserver), dependency: (disposeBag: disposeBag, networkService: serviceManager))
        viewModel.registerTableViewCell(self.feedTableView)
        viewModel.data.bind(to: self.feedTableView.rx.items) { (tableView, row, element) in
            if element.storyInfo.avatarUrl == nil {
                let cell : STFeedListStoryCell = tableView.dequeueReusableCell(withIdentifier: "\(STFeedListStoryCell.self)")! as! STFeedListStoryCell
                cell.configureCell(storyItem: element)
                cell.selectionStyle = .none
                return cell
            } else {
                let cell : STFeedListStoryWithImageCell = tableView.dequeueReusableCell(withIdentifier: "\(STFeedListStoryWithImageCell.self)")! as! STFeedListStoryWithImageCell
                cell.configureCell(storyItem: element)
                cell.selectionStyle = .none
                return cell
            }
            }
            .disposed(by: disposeBag)
        //下拉刷新状态结束的绑定
        viewModel.endHeaderRefreshing.subscribe(onNext: { (result) in
            self.feedTableView.endRefreshing(isSuccess: true)
            if result {
                self.feedTableView.endLoadMore(isNoMoreData: false)
            }
        }).disposed(by: disposeBag)
        
        //上拉刷新状态结束的绑定
        viewModel.endFooterRefreshing.subscribe(onNext: { (result) in
            self.feedTableView.endLoadMore(isNoMoreData: !result)
        }).disposed(by: disposeBag)
    }
    
    func triggerRefreshing() {
        self.feedTableView.triggerRefreshing()
    }
    
    override func updateConstraints() {
        super.updateConstraints()
        self.feedTableView.snp.updateConstraints { (make) in
            make.edges.equalToSuperview()
        }
    }
}
